<template>
	<div class="app">
		<section class="app-carousel">
			<template>
				<el-carousel class="homepage-carousel" :interval="5000" height="400px">
					<el-carousel-item v-for="(item, index) in carouselPicData" :key="index">
						<a :href="item.link_url == '' ? false : item.link_url"><img :src="BASE_URL + item.picture" /></a>
					</el-carousel-item>
				</el-carousel>
			</template>
		</section>
		<section class="app-list maxW">
			<div class="i_application" v-for="(item, index) of appData" v-show="item.listData.length" :key="index">
				<div class="i_applicationTitle clear" >
					<span>{{ item.name }}</span>
				</div>
				<div class="i_applicationBox clear">
					<a
						v-for="(item1, index1) of item.listData"
						:key="index1"
						class="i_appList wow rollIn"
						href="javascript:;"
						@click="checkRole(item1.url, item1.jump_model, item1.isdefault, item1.id, item1.true_url, item1.is_new_from)"
					>
						<div><img :src="BASE_URL + item1.logo_path" /></div>
						<p>{{ item1.name }}</p>
					</a>
				</div>
				<div :class="index == 0 ? 'pleaceholder0' : 'pleaceholder-box'" v-if="item.listData != undefined && item.listData.length == 0"></div>
			</div>
		</section>
	</div>
</template>

<script>
export default {
	props: {
		pathOption: {
			personal: '', //个人中心跳转路径
			manage: '' //后台跳转路径
		}
	},
	data() {
		return {
			carouselPicData: [], //轮播图数据
			appData: [], //单个应用标题及所包含的应用数据
		};
	},
	created() {
		this.getCarouselPic();
		this.getAppTitle();
	},
	computed: {
		CheckSchoolAppCode() {
			let code = sessionStorage.getItem('CheckSchoolAppCode');
			return code ? code : '00000000000000000000000000000000';
		}
	},
	methods: {
		//轮播图
		getCarouselPic() {
			let success = res => {
				this.carouselPicData = res.data;
			};
			this.$ajax({
				url: 'api/carousel/banners',
				method: 'get',
				params: {
					module: 5
				},
				func: {
					success: success
				}
			});
		},
		// 获取应用列表大标题
		getAppTitle() {
			let success = res => {
				this.appData = res.data;
				let _data = deep_clone(res.data);
				for (var i = 0; i < _data.length; i++) {
					this.appData[i].listData = [];
					this.getAppList(i, _data[i].id);
				}
			};
			this.$ajax({
				url: 'api/app/app_type',
				method: 'get',
				params: {},
				func: {
					success: success
				}
			});
		},
		// 根据列表标题id,去取对应的应用数据
		getAppList(inx, _id) {
			let success = res => {
				this.$set(this.appData[inx], 'listData', res.data);
				this.$forceUpdate();
			};
			this.$ajax({
				url: 'api/app/app_list',
				method: 'get',
				params: {
					page_index: 1,
					page_size: 12,
					app_type: _id,
					status: -1,
					select_type: 1,
				},
				func: {
					success: success
				}
			});
		},
		/**  应用图标的点击事件
		 * @param url 前台应用跳转地址
		 * @param jumpModel '跳转方式 1：直接跳转（本平台以内的）  2验证跳转（参考跳转到前台） 3（验证跳转到个人中心） 4 直接跳转（本平台以外的）'
		 * @param isDefault 是否是默认应用 1：是；0：不是（注默认应用不验证权限，登录 除对话，发评论等需登录的功能外）
		 * @param id 应用id
		 * @param trueUrl 其他系统模块应用跳转地址
		 * @param isNewFrom 0：当前窗口打开；1：新窗口打开
		 */
		checkRole(url, jumpModel, isDefault, id, trueUrl, isNewFrom) {
			if (url == '') {
				this.$message.error('该功能暂未开放！');
				return;
			}
			if (isDefault != 1) {
				if (this.userId == 0) {
					this.$message.error('该功能需要登录后才能使用！');
					return;
				}
				this.checkNode(url, jumpModel, isDefault, id, trueUrl, isNewFrom);
			} else {
				this.addAppVisitRecord(id);

				if (jumpModel == 1 || jumpModel == 2) {
					switch (url) {
						case '/index/resource/index':
							url = 'home/resourceList';
							break;
						case 'live':
							url = 'home/liveList';
							break;
						case 'album':
							url = 'home/albumList';
							break;
						case 'openclass':
							url = 'home/openclassList';
							break;
						case 'teachcenter':
							url = 'teach';
							break;
						case 'email':
							url = 'email/inbox';
							break;
						case 'ask':
							url = 'teach/ask';
							break;
					}
					if (isNewFrom == 0) {
						this.$router.push({
							path: '/' + url,
							query: {
								CheckSchoolAppCode: this.CheckSchoolAppCode
							}
						});
					} else {
						const { href } = this.$router.resolve({
							path: '/' + url,
							query: {
								CheckSchoolAppCode: this.CheckSchoolAppCode
							}
						});
						window.open(href, '_blank');
					}
				}
				// 跳个人中心
				else if (jumpModel == 3) {
					
					//  跳转
					let urlString = this.pathOption.personal + '&routerUrl=' + url + '&CheckSchoolAppCode=' + this.CheckSchoolAppCode;
					isNewFrom == 0 ? (window.location.href = urlString) : window.open(urlString, '_blank');
				}
				//跳其他系统模块
				else if (jumpModel == 4) {
					if (trueUrl == '' || trueUrl == null || trueUrl == false) {
						this.$message.error('该功能暂未开放！');
						return;
					}
					isNewFrom == 0 ? (window.location.href = url) : window.open(url, '_blank');
				}
			}
		},
		// 检验跳转权限
		checkNode(url, jumpModel, isDefault, id, trueUrl, isNewFrom) {
			let success = res => {
				this.addAppVisitRecord(id);
				// 跳前台
				if (jumpModel == 1 || jumpModel == 2) {
					switch (url) {
						case '/index/resource/index':
							url = 'home/resourceList';
							break;
						case 'live':
							url = 'home/liveList';
							break;
						case 'album':
							url = 'home/albumList';
							break;
						case 'openclass':
							url = 'home/openclassList';
							break;
						case 'teachcenter':
							url = 'teach';
							break;
						case 'email':
							url = 'email/inbox';
							break;
						case 'ask':
							url = 'teach/ask';
							break;
					}
					if (isNewFrom == 0) {
						
						this.$router.push(`/${url}`)
					} else {
						
						const href = `${location.origin}/${url}?CheckSchoolAppCode=${this.CheckSchoolAppCode}`
						console.log('???', href)
						window.open(href, '_blank');
					}
				}
				// 跳个人中心
				else if (jumpModel == 3) {
					let urlString = this.pathOption.personal + '&routerUrl=' + url + '&CheckSchoolAppCode=' + this.CheckSchoolAppCode;
					console.log(url, jumpModel, isDefault, id, trueUrl, isNewFrom, this.pathOption, urlString)
					isNewFrom == 0 ? (window.location.href = urlString) : window.open(urlString);
				}
				//跳其他系统模块
				else if (jumpModel == 4) {
					if (trueUrl == '' || trueUrl == null || trueUrl == false) {
						this.$message.error('该功能暂未开放！');
						return;
					}
					isNewFrom == 0 ? (window.location.href = trueUrl) : window.open(trueUrl, '_blank');
				}
			};
			this.$ajax.get('/api/login/checkthisnode', {
				params: {
					url: url,
					token: localStorage.getItem('logintoken'),
					checkschoolappcode: this.CheckSchoolAppCode
				},
				func: { success: success }
			});
		},
		/** 添加应用访问记录
		 * @param id 应用id
		 */
		addAppVisitRecord(id) {
			let success = res => { };
			this.$ajax({
				url: '/api/app/add_app_visit_record',
				method: 'post',
				data: {
					app_id: id
				},
				func: {
					success: success
				}
			});
		}
	}
};
</script>
<style lang="scss" scoped>
.app-list {
	min-height: 50vh;
}

.pleaceholder-box {
	height: 380px;
	background-color: #fff;
}

.pleaceholder0 {
	height: 190px;
	background-color: #fff;
}
</style>
